package com.tina.util;

import java.io.BufferedWriter;
import java.io.FileWriter;

/**
 * Docker封装类
 *
 * @author Tina
 * @version 1.0
 * @since 2021/11/19 09:29
 **/
public class DockerUtil {

    /**
     * 拼接Jar包Dockerfile参数
     *
     * @param port       端口
     * @param sourceFile 源文件
     * @param fileName   文件名称
     * @param jdkVersion jdk版本
     * @param author     作者
     * @return
     */
    private static StringBuilder dockerfile(int port, String sourceFile, String fileName, String jdkVersion, String author) {
        StringBuilder stringBuilder = new StringBuilder();
        stringBuilder.append("FROM " + jdkVersion + "\n");
        stringBuilder.append("MAINTAINER " + author + "\n");
        stringBuilder.append("copy " + sourceFile + " /" + fileName + "\n");
        stringBuilder.append("CMD java -jar /" + fileName + "\n");
        stringBuilder.append("EXPOSE " + port);
        return stringBuilder;
    }

    /**
     * 创建Dockerfile文件
     */
    public static StringBuilder createDockerfile() {
        StringBuilder result = new StringBuilder();
        try {

            StringBuilder cmd = dockerfile(8080, "tinaspringboot.jar", "tinaspringboot.jar", "openjdk:8-jre", "tina");
            BufferedWriter out = new BufferedWriter(new FileWriter("Dockerfile"));
            out.write(cmd.toString());
            out.close();
            result.append("创建Dockerfile 文件成功\n");
            result.append(cmd);
        } catch (Exception e) {
            e.printStackTrace();
            result.append("创建Dockerfile 文件失败\n");
            result.append(e.getMessage());
        }
        return result;
    }

    /**
     * 打包镜像
     *
     * @param imagesName
     * @return
     */
    public static String buildCmd(String imagesName) {
        String cmd = "docker build -t " + imagesName.toLowerCase() + " .";
        return cmd;
    }

    /**
     * 运行镜像
     *
     * @param instName         实例名
     * @param nodePort         外部映射端口
     * @param soucePort        内部端口
     * @param hangingDirectory 挂着目录
     * @param cpu              cpu核数
     * @param ram              内存M
     * @return cmd
     */
    public static String runCmd(String instName, String imagesName, int nodePort, int soucePort, String hangingDirectory, int cpu, int ram) {
        String cmd = "docker run -d -p " + nodePort + ":" + soucePort
                + " --name " + instName
                + " --cpuset-cpus=" + cpu
                + " -m " + ram + "M"
                + " -v " + hangingDirectory + ":" + hangingDirectory
                + " " + imagesName;
        return cmd;
    }

    /**
     * 停止实例
     *
     * @param instName 实例名
     * @return cmd
     */
    public static String stopCmd(String instName) {
        String cmd = "docker stop " + instName;
        return cmd;
    }

    /**
     * 启动实例
     *
     * @param instName 实例名
     * @return cmd
     */
    public static String startCmd(String instName) {
        String cmd = "docker start " + instName;
        return cmd;
    }

    /**
     * 重启实例
     *
     * @param instName 实例名
     * @return cmd
     */
    public static String restartCmd(String instName) {
        String cmd = "docker restart " + instName;
        return cmd;
    }

    /**
     * 删除实例
     *
     * @param instName 实例名
     * @return cmd
     */
    public static String deleteInstCmd(String instName) {
        String cmd = "docker rm " + instName;
        return cmd;
    }


    /**
     * 删除镜像
     *
     * @param instName 实例名
     * @return cmd
     */
    public static String deleteImagesCmd(String instName, String version) {
        String cmd = "docker rmi " + instName + ":" + version;
        return cmd;
    }

    /**
     * 实例状态是否运行
     *
     * @param instName 实例名
     * @return cmd
     */
    public static String stats(String instName) {
        String cmd = "docker inspect --format '{{.State.Running}}' " + instName;
        return cmd;
    }
}
